package com.hertz.iris.view;

import java.util.List;

import javax.inject.Inject;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.github.kevinsawicki.wishlist.SingleTypeAdapter;
import com.hertz.iris.Injector;
import com.hertz.iris.IrisApplication;
import com.hertz.iris.R;
import com.hertz.iris.core.NormalAlarmService;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.util.BundleKeys;
import com.hertz.iris.util.Strings;
import com.hertz.iris.view.alarm.NormalAlarmActivity;

public class NormalAlarmListAdapter extends SingleTypeAdapter<NormalAlarm> {
	@Inject protected NormalAlarmService normalAlarmService;
    /**
     * @param inflater
     * @param items
     */
    public NormalAlarmListAdapter(LayoutInflater inflater, List<NormalAlarm> items) {
        super(inflater, R.layout.normalalarm_list_item);
        Injector.inject(this);
        setItems(items);
    }

    /**
     * @param inflater
     */
    public NormalAlarmListAdapter(LayoutInflater inflater) {
        this(inflater, null);

    }

    @Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = super.getView(position, convertView, parent);
		LinearLayout normalAlarmInfoLL = (LinearLayout)view.findViewById(R.id.normal_alarm_info_ll);
		LinearLayout normalAlarmTimeLL = (LinearLayout)view.findViewById(R.id.normal_alarm_time_ll);
		final NormalAlarm na = getItem(position);
		if(na.getIsActive()){
			normalAlarmTimeLL.setBackgroundResource(R.drawable.bbuton_info);
		}else{
			normalAlarmTimeLL.setBackgroundResource(R.color.bbutton_info_disabled);
		}
		normalAlarmInfoLL.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), NormalAlarmActivity.class);
				intent.putExtra(BundleKeys.NormalAlarmBundleKeys.NORMAL_ALARM_KEY, na);
				v.getContext().startActivity(intent);
			}
		});
		normalAlarmTimeLL.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(na != null){
					if(na.getIsActive()){
						na.setIsActive(false);
						v.setBackgroundResource(R.color.bbutton_info_disabled);
					}else{//start
						na.setIsActive(true);
						v.setBackgroundResource(R.drawable.bbuton_info);
					}
					normalAlarmService.resetAlarmActive(na);
					normalAlarmService.updateNormalAlarm(na);
				}
			}
		});
		return view;
	}

	@Override
    public long getItemId(final int position) {
        return super.getItemId(position);
    }

    @Override
    protected int[] getChildViewIds() {
        return new int[] {R.id.alarm_name_text,R.id.alarm_info_text,R.id.alarm_time_text};
    }

    @Override
    protected void update(int position, NormalAlarm normalAlarm) {

        setText(0, Strings.isBlank(normalAlarm.getName()) ? IrisApplication.getInstance().getResources().getString(R.string.alarm_name_default) : normalAlarm.getName());
        setText(1,getLoopValue(normalAlarm.getWeeks()));
        setText(2,String.format("%02d : %02d", normalAlarm.getHh(),normalAlarm.getMm()));

    }

    private String getLoopValue(String weeksStr){
    	String[] values = weeksStr.split(",");
		StringBuffer sb = new StringBuffer();
		String[] weeks = IrisApplication.getInstance().getResources().getStringArray(R.array.weeks);
		for(int i = 0; i < values.length; i++){
			if(values[i].equals("1")){
				sb.append(weeks[i]).append(" ");
			}
		}
		return sb.toString();
	}
}
